//
// Created by jiao on 2021/4/15.
//

#ifndef ALOOPERDEMO_YHTHREADHANDLER_H
#define ALOOPERDEMO_YHTHREADHANDLER_H

#include <sys/types.h>
#include "vector"
#include <sys/epoll.h>
#include "jni.h"
#include "YHHandler.h"

class YHThreadHandler : public YHHandler {
public:
    YHThreadHandler(YqHandlerCallBackFunc *callBack);

    ~YHThreadHandler();

    void sendMessage(int ident, void *data);

    void removeMessage(int ident);

    void removeAll();

    void quit();

private:
    void destroy();

    void backgroundRun();

    void wake();

    void awoken();

    static void *threadFunc(void *p);


    volatile bool mPolling = false;
    volatile bool isQuit = false;
    pthread_mutex_t mLooper_mutex_{};
    YqHandlerCallBackFunc *mCallBack;
    std::vector<std::pair<int, void *>> mQueue;
    int mEventFd;
    int mEpollFd;
};


#endif //ALOOPERDEMO_YHTHREADHANDLER_H
